import time
from datetime import datetime
import pyttsx3
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from pydub import AudioSegment
from pydub.playback import play

def alarm():
    song = AudioSegment.from_wav('老街.wav')
    pp = pyttsx3.init()
    alarm_time = input("输入要设置的闹钟时间：")
    alarm_period = input("输入要设置的时期AM/PM:")
    alarm_hour = alarm_time[0:2]#获取小时
    alarm_minute = alarm_time[3:5]#获取分钟
    alarm_seconds = alarm_time[6:8]#获取秒
    print('设置成功，服务正在运行')
    flag = True
    while flag:
        now = datetime.now()
        current_hour = now.strftime("%H")
        current_minute = now.strftime("%M")
        current_seconds = now.strftime("%S")
        current_period = now.strftime("%p")
        if(alarm_hour==current_hour and alarm_minute == current_minute and alarm_seconds == current_seconds and alarm_period == current_period):
            print("醒来")
            play(song)
            if(int(now.strftime('%S'))-int(alarm_seconds)==10):#间隔十秒钟
                play(song)
                time.sleep(5)
                pp.say("不起来吗？你逼我的！")
                pp.runAndWait()
    return 1

def mail_qq():
    my_sender = "1006170239@qq.com"#发件人邮箱
    my_pass = 'brbkiuyirjnjbeab'#授权码
    my_user = '1923519335@qq.com'#收件人邮箱

    def mail():
        ret = True
        try:
            msg = MIMEText('起不来只想睡，谁爱上班谁上','plain','utf-8')#填写内容
            msg['From'] = formataddr(['今天去哪玩',my_sender])#发件人邮箱昵称，账号
            msg['To'] = formataddr(['寻不尽天下',my_user])#收件人对应邮箱昵称，账号
            msg['subject'] = '辞职报告'#邮件主题

            server = smtplib.SMTP_SSL('smtp.qq.com',465)#发件人邮箱中的SMTP服务器
            server.login(my_sender,my_pass)#括号中对应的是发件人邮箱账号，邮箱密码
            server.sendmail(my_sender,[my_user,],msg.as_string())#括号中对应发件人邮箱账号，收件人邮箱账号，发送邮件
            server.quit()
        except Exception:
            ret = False
        return ret
    ret = mail()
    if ret:
        print('邮箱发送成功')
    else:
        print('发送失败')

if __name__ == '__main__':
    a = alarm()
    if a==1:
        mail_qq()